package com.oracle.member.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.oracle.mapper.MemberMapper;
import com.oracle.member.service.api.MemberServiceApi;

import com.oracle.pojo.Member;
import com.oracle.pojo.dto.MemberDTO;
import com.oracle.third.service.email.api.EmailServiceApi;
import com.oracle.third.service.mobile.api.MobileServiceApi;
import com.oracle.vo.ResultEntity;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;

import java.sql.Timestamp;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Service
public class MemberServiceImpl implements MemberServiceApi {

    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private MobileServiceApi mobileServiceApi;
    @Autowired
    private EmailServiceApi emailServiceApi;

    //email服务队列
    @Value("${email_exchange}")
    private String email_exchange;
    @Value("${email_queue}")
    private String email_queue;
    @Value("${email_bindKey}")
    private String email_bindKey;
    //手机短信服务消息队列
    @Value("${mobile_exchange}")
    private String mobile_exchange;
    @Value("${mobile_queue}")
    private String mobile_queue;
    @Value("${mobile_bindKey}")
    private String mobile_bindKey;


    @Override
    public ResultEntity add(Member member) {
        if (member.getUsername() == null && "".equals(member.getUsername())) {
            return new ResultEntity(ResultEntity.CODE_FAIL, "用户名参数不能为空");
        }
        if (member.getLoginname() == null && "".equals(member.getLoginname())) {
            return new ResultEntity(ResultEntity.CODE_FAIL, "登录名参数不能为空");
        }
        if (member.getPwd() == null && "".equals(member.getPwd())) {
            return new ResultEntity(ResultEntity.CODE_FAIL, "密码参数不能为空");
        }
        int count = this.memberMapper.selectByLoginNameCount(member.getLoginname());
        if (count > 0) {
            return new ResultEntity(ResultEntity.CODE_FAIL, "登录名已存在，不可重复注册。");
        }
        //头像参数暂时不写非空判断

        member.setCreatedate(new Timestamp(new Date().getTime()));
        member.setModifydate(new Timestamp(new Date().getTime()));
        member.setStatus(0);
        this.memberMapper.insertSelective(member);
        return new ResultEntity(ResultEntity.CODE_SUCCESS, ResultEntity.MSG_SUCCESS);
    }

    @Override
    public ResultEntity checkedLoginName(String loginname) {
        int count = this.memberMapper.selectByLoginNameCount(loginname);
        if (count == 0) {
            return new ResultEntity(ResultEntity.CODE_SUCCESS, "正常");
        }
        return new ResultEntity(ResultEntity.CODE_FAIL, "已存在");
    }

    @Override
    public ResultEntity checkedEmail(String email) {
        int count = this.memberMapper.selectByEmailCount(email);
        if (count == 0) {
            return new ResultEntity(ResultEntity.CODE_SUCCESS, "正常");
        }
        return new ResultEntity(ResultEntity.CODE_FAIL, "已存在");
    }

    @Override
    public ResultEntity checkedPhone(String phone) {
        int count = this.memberMapper.selectByPhoneCount(phone);
        if (count == 0) {
            return new ResultEntity(ResultEntity.CODE_SUCCESS, "正常");
        }
        return new ResultEntity(ResultEntity.CODE_FAIL, "已存在");
    }

    @Override
    public ResultEntity createEmailCode(String email) {
        //判断email是否已存在
        int count = this.memberMapper.selectByEmailCount(email);
        if (count == 0) {
            //如果不存在，再判断是否可以发送验证码；
            ResultEntity resultEntity = this.emailServiceApi.checkedCanReSndRandomCode(email);
            if (resultEntity.getCode().equals(ResultEntity.CODE_SUCCESS)) {
                //发送消息到消息中间件
                this.rabbitTemplate.convertAndSend(email_exchange, email_bindKey, email);
                return new ResultEntity(ResultEntity.CODE_SUCCESS, ResultEntity.MSG_SUCCESS);
            } else {
                return new ResultEntity(ResultEntity.CODE_FAIL, "暂时无法发送验证，请稍后再试");
            }
        }
        return new ResultEntity(ResultEntity.CODE_FAIL, "不可重复注册");
    }

    @Override
    public ResultEntity createModileCode(String phone) {
        int count = this.memberMapper.selectByPhoneCount(phone);
        if (count == 0) {
            //如果不存在，再判断是否可以发送验证码；
            ResultEntity resultEntity = this.mobileServiceApi.checkedCanReSndRandomCode(phone);
            if (resultEntity.getCode().equals(ResultEntity.CODE_SUCCESS)) {
                this.rabbitTemplate.convertAndSend(mobile_exchange, mobile_bindKey, phone);
                return new ResultEntity(ResultEntity.CODE_SUCCESS, ResultEntity.MSG_SUCCESS);
            } else {
                return new ResultEntity(ResultEntity.CODE_FAIL, "暂时无法发送验证，请稍后再试");
            }
        }
        return new ResultEntity(ResultEntity.CODE_FAIL, "不可重复注册");
    }

    @Override
    public ResultEntity mobileReg(MemberDTO memberDTO) {
        System.out.println("--------serviceImpl层-------->" + memberDTO);
        //判断数据是否为空
        if (memberDTO.getUsername() == null || "".equals(memberDTO.getUsername())) {

            return new ResultEntity(201, "用户名不能为空");
        }
        if (memberDTO.getLoginname() == null || "".equals(memberDTO.getLoginname())) {
            return new ResultEntity(201, "登录名不能为空");
        }
        if (memberDTO.getPwd() == null || "".equals(memberDTO.getPwd())) {
            return new ResultEntity(201, "密码不能为空");
        }
        if (memberDTO.getPhone() == null || "".equals(memberDTO.getPhone())) {
            return new ResultEntity(201, "手机号不能为空");
        }
        //二次判断用户名和手机号是否被注册过
        int loginNameCount = this.memberMapper.selectByLoginNameCount(memberDTO.getLoginname());
        int phoneCount = this.memberMapper.selectByPhoneCount(memberDTO.getPhone());
        if (phoneCount > 0 || loginNameCount > 0) {
            return new ResultEntity(ResultEntity.CODE_FAIL, "不可重复注册");
        }
        //没有重复注册问题进行验证码的判断
        ResultEntity resultEntity = this.mobileServiceApi.checkedRandomCode(memberDTO.getPhone(), memberDTO.getRequestCode());
        if (resultEntity.getCode().equals(ResultEntity.CODE_SUCCESS)) {
            //验证码正确，执行注册操作
            Member member = new Member();
            BeanUtils.copyProperties(memberDTO, member);
            member.setStatus(0);
            member.setModifydate(new Timestamp(new Date().getTime()));
            member.setCreatedate(new Timestamp(new Date().getTime()));
            this.memberMapper.insertSelective(member);
            return new ResultEntity(ResultEntity.CODE_SUCCESS, ResultEntity.MSG_SUCCESS);
        }
        return new ResultEntity(ResultEntity.CODE_FAIL, "验证码错误或已失效");
    }

    @Override
    public ResultEntity emailReg(MemberDTO memberDTO) {
        //判断数据是否为空
        if (memberDTO.getUsername() == null || "".equals(memberDTO.getUsername())) {

            return new ResultEntity(201, "用户名不能为空");
        }
        if (memberDTO.getLoginname() == null || "".equals(memberDTO.getLoginname())) {
            return new ResultEntity(201, "登录名不能为空");
        }
        if (memberDTO.getPwd() == null || "".equals(memberDTO.getPwd())) {
            return new ResultEntity(201, "密码不能为空");
        }
        if (memberDTO.getEmail() == null || "".equals(memberDTO.getEmail())) {
            return new ResultEntity(201, "邮箱地址不能为空");
        }
        //二次判断用户名和邮箱是否被注册过
        int emailCount = this.memberMapper.selectByEmailCount(memberDTO.getEmail());
        int loginNameCount = this.memberMapper.selectByLoginNameCount(memberDTO.getLoginname());
        if (emailCount > 0 || loginNameCount > 0) {
            return new ResultEntity(ResultEntity.CODE_FAIL, "不可重复注册");
        }
        //没有重复注册问题进行验证码的判断
        ResultEntity resultEntity = this.emailServiceApi.checkedRandomCode(memberDTO.getEmail(), memberDTO.getRequestCode());
        if (resultEntity.getCode().equals(ResultEntity.CODE_SUCCESS)) {
            //验证码正确，执行注册操作
            Member member = new Member();
            BeanUtils.copyProperties(memberDTO, member);
            member.setStatus(0);
            member.setModifydate(new Timestamp(new Date().getTime()));
            member.setCreatedate(new Timestamp(new Date().getTime()));
            this.memberMapper.insertSelective(member);
            return new ResultEntity(ResultEntity.CODE_SUCCESS, ResultEntity.MSG_SUCCESS);
        }
        return new ResultEntity(ResultEntity.CODE_FAIL, "验证码错误或已失效");
    }


}
